//var domain_url = "http://localhost/ilovebt/";
var domain_url = "http://www.yyste.com/";

//function LoadJS( file_ ){
//    //should add domain here, or admin pages can not work
//    document.write( "<script type=\"text/javascript\" src=\""+domain_url+"static/js/" + file_ +   "\"><\/script>" );
//}
//
//LoadJS( "lib/jquery-1.7.min.js" );

$(document).ready(function(){
    var title = $("#title").val();
    $.getJSON("https://ajax.googleapis.com/ajax/services/search/images?callback=?",
    {
        v : '1.0',
        rsz : '5',
        imgsz: 'large|xlarge|xxlarge|huge',
        q : title
    },
    function(data) {
        $.each(data.responseData.results, function(i,item){
            $("<img/>").attr("src", item.url).appendTo("#images");
            $("<br />").appendTo("#images");
        });
    });
    
    // set xunlei url
    $(".xl_cloud_btn").click( function(){
        window.open( 'http://vod.xunlei.com/home.html?playurl='+encodeURIComponent( $(this).attr( 'magurl') ));
    });
    
    // request douban API
    var imgurl = $('#largeimgurl').val();
    var itemId = $('#itemid').val();
    if( imgurl == '' ){
        var title = $('#title').val();
        $.getJSON("https://api.douban.com/v2/movie/search?callback=?",
        {
            start : 0,
            count : 1,
            q : title
        },
        function(data) {
            if( typeof(data) != 'undefined' && data.count >= 1 ){
                if( typeof data.subjects[0] != 'undefined' ){
                    // update item image url when api call succeed
                    $.getJSON( domain_url+"index.php?callback=?", 
                    {
                        action : 'updateItemDetail',
                        id : itemId,
                        largeimgurl : data.subjects[0].images.large,
                        doubanrating : data.subjects[0].rating.average,
                        doubansubtype : data.subjects[0].subtype,
                        doubanid : data.subjects[0].id,
                        year : data.subjects[0].originaltitle,
                        originaltitle : data.subjects[0].original_title
                    },
                    function(data) {
                        // alert( JSON.stringify(data) );
                        // we don't care about the result
                    });
                }
            }
        });
    }
});

